package com.huluobo.high3videoproject.mvp.login.presenter;

import com.huluobo.base.view.BasePresenter;
import com.huluobo.high3videoproject.bean.UserBeanResult;
import com.huluobo.high3videoproject.mvp.login.model.LoginModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    private LoginModel loginModel = new LoginModel();

    @Override
    public void login(String username, String password) {
        loginModel.login(username, password).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(UserBeanResult userBeanResult) {
                        if (isAttach()) {
                            getView().onLoginSuccess(userBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onLoginFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
